<template>
<div>
    <LoginHeader>欢迎登陆</LoginHeader>
    <!-- <div class="login-box">
        <div class="title">
           <b>超市购物平台</b>
        </div>
        <div style="margin-top:30px">
            <el-form label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model.trim="user.username" aria-required="true"></el-input>
                </el-form-item>
                <el-form-item label="密码" style="margin-top: 25px">
                    <el-input v-model.trim="user.password" show-password aria-required="true"></el-input>
                </el-form-item>
                <el-form-item style="margin: 30px 80px">
                    <el-button type="success" @click="onSubmit">登录</el-button>
                    <el-button @click="$router.push('/register')">注册</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div> -->
    <section class="login-section">
    <div class="wrapper">
      <nav>
        <a href="javascript:;" @click="activeName = 'account'" :class="{active:activeName === 'account'}">账户登录</a>
        <a href="javascript:;" @click="activeName = 'phone'"  :class="{active:activeName === 'phone'}">手机号登录</a>
      </nav>
      <LoginForm v-if="activeName === 'account'"/>
      <LoginFormAccount v-if="activeName === 'phone'"/>
    </div>
  </section>
    <LoginFooter></LoginFooter>
</div>
</template>
<!--页面的数据和方法-->

<script>
import LoginHeader from './components/loginHeader'
import LoginFooter from './components/loginFooter'
import LoginForm from './components/loginForm.vue'
import LoginFormAccount from './components/loginFormAccount.vue'
export default {
 name:"Login",
 components:{LoginHeader, LoginFooter, LoginForm, LoginFormAccount},
//    给组件命名
    data(){
    // 组件的初始状态
   return{
        to:'/',
       user: {},
       activeName: 'account'
   }
    },
    created() {
    // 组件被创建后会执行此函数  检查路由参数，判断登陆成功后应跳转的页面
   this.to =this.$route.query.to?this.$route.query.to:"/"
    },
    methods: {
        onSubmit() {
            // 检查输入的用户名或密码是否为空
            if (this.user.username === '' || this.user.password === '') {
                // 使用 Element UI 的消息组件给出错误提示
                this.$message.error("账号或密码不能为空");
                return false; // 结束函数执行
            }

            // 发起 POST 请求到服务器的 /login 接口，并传递用户的用户名和密码
            this.request.post("/login", this.user).then(res => {
                // 服务器响应后的处理

                // 检查返回的状态码是否为 '200'，即请求成功
                if (res.code === '200') {
                    // 显示成功消息提示
                    this.$message.success({message: "登陆成功", showClose: true});

                    // 跳转到预定的页面，可能是主页或之前访问的页面
                    this.$router.push(this.to);

                    // 将服务器返回的用户数据保存到浏览器的 localStorage
                    localStorage.setItem("user", JSON.stringify(res.data));
                } else {
                    // 如果返回的状态码不是 '200'，则显示服务器返回的错误消息
                    this.$message.error(res.msg);
                }
            });
        }
    }




}

</script>


<style scoped lang="less">
.login-section {
  background: url(../../assets/images/login-bg.png) no-repeat center / cover;
  height: 488px;
  position: relative;
  .wrapper {
    width: 380px;
    background: #fff;
    min-height: 400px;
    position: absolute;
    left: 50%;
    top: 54px;
    transform: translate3d(100px,0,0);
    box-shadow: 0 0 10px rgba(0,0,0,.15);
    nav {
      height: 55px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;  
      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        &:first-child {
          border-right: 1px solid #f5f5f5;
          text-align: left;
        }
        &.active{
          font-weight: bold;
          color:#27BA9B;
        }
      }
    }
  }
}
</style>